package com.jiuli.liping.service.impl;



import com.jiuli.liping.dao.MoveHisDao;
import com.jiuli.liping.dao.ProductDao;
import com.jiuli.liping.dao.ProductOrderDao;
import com.jiuli.liping.dao.UserlistDao;
import com.jiuli.liping.service.ToOaService;
import com.jiuli.liping.util.JaxWsDynamicClientFactory;
import com.jiuli.liping.util.XmlUtil;
import com.jiuli.liping.vo.*;
import org.apache.cxf.endpoint.Client;

import org.apache.cxf.frontend.ClientProxy;
import org.apache.cxf.transport.http.HTTPConduit;
import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.namespace.QName;



import java.net.MalformedURLException;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import com.jiuli.liping.mypackage.WorkflowServiceXmlPortType;
import org.springframework.transaction.annotation.Transactional;

/*import static javafx.scene.input.KeyCode.F;
import static javafx.scene.input.KeyCode.S;
import static javax.print.attribute.standard.MediaSizeName.A;
import static javax.print.attribute.standard.MediaSizeName.D;*/

@Service
public class ToOaServiceImpl implements ToOaService {
    public final static String oa_url = "http://192.168.5.20:99/services/WorkflowServiceXml?wsdl";
    @Autowired
    private UserlistDao userlistDao;
    @Autowired
    private ProductDao productDao;
    @Autowired
    private MoveHisDao moveHisDao;
    @Autowired
    private ProductOrderDao productOrderDao;
    @Override
    public Message StartFlow(List<OaMessage> oaMessages) {
        boolean isOverNumber=false;
        Message message=new Message();
        float Maxprice=0;
        float MaxPurchasePrice=0;
        float price=0;
        List<HrmResource> hrmResources=new ArrayList<>();
        for (int i=0;i<oaMessages.size();i++){
            int x=productDao.getRealProductNumber(oaMessages.get(i).getFbk1());
            int y=productDao.getRealProductNumberx(oaMessages.get(i).getFbk1());
            int realProductNumber = x-y;
            int iceProductNumber = productDao.getIceProductNumber(oaMessages.get(i).getFbk1());
            price=price+Integer.parseInt(oaMessages.get(i).getProductNumber())*Float.parseFloat(productDao.getPurchasePrice(oaMessages.get(i).getFbk1()));
            oaMessages.get(i).setKoujnumber(iceProductNumber+Integer.parseInt(oaMessages.get(i).getProductNumber())+"");
            if (Float.parseFloat(oaMessages.get(i).getProductPrice())>Maxprice){
                Maxprice=Float.parseFloat(oaMessages.get(i).getProductPrice());
            }else {
                Maxprice=Maxprice;
            }
            if (Float.parseFloat(productDao.getPurchasePrice(oaMessages.get(i).getFbk1()))>MaxPurchasePrice){
                MaxPurchasePrice=Float.parseFloat(productDao.getPurchasePrice(oaMessages.get(i).getFbk1()));
            }else {
                MaxPurchasePrice=MaxPurchasePrice;
            }

            if ((realProductNumber-iceProductNumber)<Integer.parseInt(oaMessages.get(i).getProductNumber())){
                isOverNumber=true;
            }
        }

        String oaId;
        String doaId="";
        if (oaMessages.get(0).getUserid().contains("S")){
            oaId = userlistDao.getOaId(oaMessages.get(0).getUserid()+"");

        }else if (oaMessages.get(0).getUserid().length()>5){
            oaId = userlistDao.getOaId(oaMessages.get(0).getUserid()+"");


        }else {
            String format = String.format("%0" + 4 + "d", Integer.parseInt(oaMessages.get(0).getUserid()));
            oaId = userlistDao.getOaId(format);
        }

        String userName = userlistDao.getUserName(String.format("%05d",Integer.valueOf(oaMessages.get(0).getUserid())));

        if ("".equals(oaMessages.get(0).getDsqrgh())){
            doaId=oaId;
            if (oaMessages.get(0).getUserid().contains("S")){
                 hrmResources = productOrderDao.selectUserName(oaMessages.get(0).getUserid() + "");
            }else if (oaMessages.get(0).getUserid().length()>5){
                 hrmResources = productOrderDao.selectUserName(oaMessages.get(0).getUserid() + "");
            }else {
                String formatx = String.format("%0" + 4 + "d", Integer.parseInt(oaMessages.get(0).getUserid()+""));
                hrmResources = productOrderDao.selectUserName(formatx+ "");
            }
            oaMessages.get(0).setDsqgs(hrmResources.get(0).getHrmResourceList().get(0).getSubcompanyid1()+"");
            oaMessages.get(0).setDsqbm(hrmResources.get(0).getHrmResourceList().get(0).getDepartmentid()+"");

        }else {
            if (oaMessages.get(0).getDsqrgh().contains("S")){
                doaId=userlistDao.getOaId(oaMessages.get(0).getDsqrgh()+"");
            }else if (oaMessages.get(0).getDsqrgh().length()>5){
                doaId=userlistDao.getOaId(oaMessages.get(0).getDsqrgh()+"");
            }else {
                if (!"".equals(oaMessages.get(0).getDsqrgh())){
                    String formatx = String.format("%0" + 4 + "d", Integer.parseInt(oaMessages.get(0).getDsqrgh()+""));
                    doaId=userlistDao.getOaId(formatx);
                }
            }
        }

        if ("".equals(oaMessages.get(0).getDsqrgh())){
            oaMessages.get(0).setDsqrgh(oaMessages.get(0).getUserid());
        }
        if (isOverNumber){
            message.setFlag("failed");
            message.setMessage("超过库存,发起oa流程失败");
        }else if(oaId==null){
            message.setFlag("failed");
            message.setMessage("未查询到oa账号,无法发起");
        }else if(doaId==null){
            message.setFlag("failed");
            message.setMessage("未查询到oa账号,无法发起");
        }else {

                WorkflowRequestInfo workflowRequestInfo = new WorkflowRequestInfo();//工作流程请求信息
                // int userid = 143;//用户ID
                workflowRequestInfo.setCanView(true);//显示
                workflowRequestInfo.setCanEdit(true);//可编辑
                workflowRequestInfo.setRequestName("商城物品领用审批-"+userName);//请求标题
                workflowRequestInfo.setRequestLevel("0");//请求重要级别 0：正常 1：重要 2：紧急
                workflowRequestInfo.setCreatorId(oaId);//创建者ID 创建流程时为必输项

                WorkflowBaseInfo workflowBaseInfo = new WorkflowBaseInfo();//工作流信息
                workflowBaseInfo.setWorkflowId("570");//流程ID
                workflowBaseInfo.setWorkflowName("商城物品领用申请");//流程名称
                workflowBaseInfo.setWorkflowTypeName("办公室");//流程类型名称
                workflowRequestInfo.setWorkflowBaseInfo(workflowBaseInfo);

                WorkflowMainTableInfo workflowMainTableInfo = new WorkflowMainTableInfo();//主表
                WorkflowRequestTableRecord[] workflowRequestTableRecord = new WorkflowRequestTableRecord[1];
                WorkflowRequestTableField[] workflowRequestTableField = new WorkflowRequestTableField[17];
                workflowRequestTableField[0] = new WorkflowRequestTableField();
                workflowRequestTableField[0].setFieldName("gonghao");
                workflowRequestTableField[0].setFieldValue(oaMessages.get(0).getUserid());
                workflowRequestTableField[0].setView(true);
                workflowRequestTableField[0].setEdit(true);


                workflowRequestTableField[1] = new WorkflowRequestTableField();
                workflowRequestTableField[1].setFieldName("beizhu");
                workflowRequestTableField[1].setFieldValue(oaMessages.get(0).getRemarks());
                workflowRequestTableField[1].setView(true);
                workflowRequestTableField[1].setEdit(true);

                workflowRequestTableField[2] = new WorkflowRequestTableField();
                workflowRequestTableField[2].setFieldName("sqbmen");
                workflowRequestTableField[2].setFieldValue(oaMessages.get(0).getBumen());
                workflowRequestTableField[2].setView(true);
                workflowRequestTableField[2].setEdit(true);

                workflowRequestTableField[3] = new WorkflowRequestTableField();
                workflowRequestTableField[3].setFieldName("sqr");
                workflowRequestTableField[3].setFieldValue(oaMessages.get(0).getUserName());
                workflowRequestTableField[3].setView(true);
                workflowRequestTableField[3].setEdit(true);

                workflowRequestTableField[4] = new WorkflowRequestTableField();
                workflowRequestTableField[4].setFieldName("lpdbh");
                workflowRequestTableField[4].setFieldValue(oaMessages.get(0).getNumber());
                workflowRequestTableField[4].setView(true);
                workflowRequestTableField[4].setEdit(true);

                workflowRequestTableField[5] = new WorkflowRequestTableField();
                workflowRequestTableField[5].setFieldName("XJzje");
                workflowRequestTableField[5].setFieldValue(oaMessages.get(0).getZongjine());
                workflowRequestTableField[5].setView(true);
                workflowRequestTableField[5].setEdit(true);

            workflowRequestTableField[6] = new WorkflowRequestTableField();
            workflowRequestTableField[6].setFieldName("dsqrgh");
            workflowRequestTableField[6].setFieldValue(oaMessages.get(0).getDsqrgh());
            workflowRequestTableField[6].setView(true);
            workflowRequestTableField[6].setEdit(true);

            workflowRequestTableField[7] = new WorkflowRequestTableField();
            workflowRequestTableField[7].setFieldName("XJfycdr");
            workflowRequestTableField[7].setFieldValue(doaId+"");
            workflowRequestTableField[7].setView(true);
            workflowRequestTableField[7].setEdit(true);

            workflowRequestTableField[8] = new WorkflowRequestTableField();
            workflowRequestTableField[8].setFieldName("XJfycdbm");
            workflowRequestTableField[8].setFieldValue(oaMessages.get(0).getDsqbm());
            workflowRequestTableField[8].setView(true);
            workflowRequestTableField[8].setEdit(true);

            workflowRequestTableField[9] = new WorkflowRequestTableField();
            workflowRequestTableField[9].setFieldName("XJfycdgs");
            workflowRequestTableField[9].setFieldValue(oaMessages.get(0).getDsqgs());
            workflowRequestTableField[9].setView(true);
            workflowRequestTableField[9].setEdit(true);

            workflowRequestTableField[10] = new WorkflowRequestTableField();
            workflowRequestTableField[10].setFieldName("sqrgs");
            workflowRequestTableField[10].setFieldValue(oaMessages.get(0).getSqrgs());
            workflowRequestTableField[10].setView(true);
            workflowRequestTableField[10].setEdit(true);

            workflowRequestTableField[11] = new WorkflowRequestTableField();
            workflowRequestTableField[11].setFieldName("dhzdje");
            workflowRequestTableField[11].setFieldValue(Maxprice+"");
            workflowRequestTableField[11].setView(true);
            workflowRequestTableField[11].setEdit(true);

            workflowRequestTableField[12] = new WorkflowRequestTableField();
            workflowRequestTableField[12].setFieldName("sfzfgm");
            workflowRequestTableField[12].setFieldValue(oaMessages.get(0).getGongyong());
            workflowRequestTableField[12].setView(true);
            workflowRequestTableField[12].setEdit(true);

            workflowRequestTableField[13] = new WorkflowRequestTableField();
            workflowRequestTableField[13].setFieldName("rkje");
            workflowRequestTableField[13].setFieldValue(MaxPurchasePrice+"");
            workflowRequestTableField[13].setView(true);
            workflowRequestTableField[13].setEdit(true);


            workflowRequestTableField[14] = new WorkflowRequestTableField();
            workflowRequestTableField[14].setFieldName("rkzje");
            workflowRequestTableField[14].setFieldValue(price+"");
            workflowRequestTableField[14].setView(true);
            workflowRequestTableField[14].setEdit(true);

            workflowRequestTableField[15] = new WorkflowRequestTableField();
            workflowRequestTableField[15].setFieldName("sf2");
            if("1".equals(oaMessages.get(0).getSf2())){
                workflowRequestTableField[15].setFieldValue(null);
            }else {
                workflowRequestTableField[15].setFieldValue(oaMessages.get(0).getSf2());
            }
            workflowRequestTableField[15].setView(true);
            workflowRequestTableField[15].setEdit(true);

            workflowRequestTableField[16] = new WorkflowRequestTableField();
            workflowRequestTableField[16].setFieldName("splb");
            workflowRequestTableField[16].setFieldValue(oaMessages.get(0).getSplb());
            workflowRequestTableField[16].setView(true);
            workflowRequestTableField[16].setEdit(true);


                workflowRequestTableRecord[0] = new WorkflowRequestTableRecord();
                workflowRequestTableRecord[0].setWorkflowRequestTableFields(workflowRequestTableField);
                workflowMainTableInfo.setRequestRecords(workflowRequestTableRecord);

                workflowRequestInfo.setWorkflowMainTableInfo(workflowMainTableInfo);


                WorkflowDetailTableInfo[] workflowDetailTableInfo = new WorkflowDetailTableInfo[1];
                WorkflowRequestTableRecord[] workflowRequestTableRecordDetail = new WorkflowRequestTableRecord[oaMessages.size()];
                for(int i=0;i<oaMessages.size();i++){
                    WorkflowRequestTableField[] workflowRequestTableFieldDetail = new WorkflowRequestTableField[6];
                    workflowRequestTableFieldDetail[0] = new WorkflowRequestTableField();
                    workflowRequestTableFieldDetail[0].setFieldName("lpmc");
                    workflowRequestTableFieldDetail[0].setFieldValue(oaMessages.get(i).getProductName());
                    workflowRequestTableFieldDetail[0].setView(true);
                    workflowRequestTableFieldDetail[0].setEdit(true);

                    workflowRequestTableFieldDetail[1] = new WorkflowRequestTableField();
                    workflowRequestTableFieldDetail[1].setFieldName("sl");
                    workflowRequestTableFieldDetail[1].setFieldValue(oaMessages.get(i).getProductNumber());
                    workflowRequestTableFieldDetail[1].setView(true);
                    workflowRequestTableFieldDetail[1].setEdit(true);

                    workflowRequestTableFieldDetail[2] = new WorkflowRequestTableField();
                    workflowRequestTableFieldDetail[2].setFieldName("dj2");
                    workflowRequestTableFieldDetail[2].setFieldValue(oaMessages.get(i).getProductPrice());
                    workflowRequestTableFieldDetail[2].setView(true);
                    workflowRequestTableFieldDetail[2].setEdit(true);

                    workflowRequestTableFieldDetail[3] = new WorkflowRequestTableField();
                    workflowRequestTableFieldDetail[3].setFieldName("je2");
                    workflowRequestTableFieldDetail[3].setFieldValue(Float.parseFloat(oaMessages.get(i).getProductPrice())*Float.parseFloat(oaMessages.get(i).getProductNumber())+"");
                    workflowRequestTableFieldDetail[3].setView(true);
                    workflowRequestTableFieldDetail[3].setEdit(true);

                    workflowRequestTableFieldDetail[4] = new WorkflowRequestTableField();
                    workflowRequestTableFieldDetail[4].setFieldName("spsx");
                    workflowRequestTableFieldDetail[4].setFieldValue(oaMessages.get(i).getSpsx());
                    workflowRequestTableFieldDetail[4].setView(true);
                    workflowRequestTableFieldDetail[4].setEdit(true);

                    workflowRequestTableFieldDetail[5] = new WorkflowRequestTableField();
                    workflowRequestTableFieldDetail[5].setFieldName("rkjed");
                    workflowRequestTableFieldDetail[5].setFieldValue(productDao.getPurchasePrice(oaMessages.get(i).getFbk1())+"");
                    workflowRequestTableFieldDetail[5].setView(true);
                    workflowRequestTableFieldDetail[5].setEdit(true);

                    workflowRequestTableRecordDetail[i] = new WorkflowRequestTableRecord();
                    workflowRequestTableRecordDetail[i].setWorkflowRequestTableFields(workflowRequestTableFieldDetail);
                }
                workflowDetailTableInfo[0] = new WorkflowDetailTableInfo();
                workflowDetailTableInfo[0].setWorkflowRequestTableRecords(workflowRequestTableRecordDetail);

                workflowRequestInfo.setWorkflowDetailTableInfos(workflowDetailTableInfo);
                String requestInfo = "";
                XmlUtil xmlu = XmlUtil.getInstance();

                try {
                    requestInfo = xmlu.objToXml(workflowRequestInfo);
                } catch (Exception e) {
                    e.printStackTrace();
                }

                String url = oa_url;
                JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();

                Client client = dcf.createClient(url);

            HTTPConduit conduit = (HTTPConduit) client.getConduit();
            HTTPClientPolicy policy = new HTTPClientPolicy();
            policy.setConnectionTimeout(30000); //连接超时时间
            policy.setReceiveTimeout(180000);//请求超时时间.
            conduit.setClient(policy);

                System.out.println("返回数据4:" + client);
                QName name = new QName("webservices.services.weaver.com.cn", "doCreateWorkflowRequest");
                Object[] objects=new Object[5];
                objects[0]=1;

                try {
                    System.out.println("返回数据1:" + name);
                    System.out.println("返回数据2:" + requestInfo);
                    System.out.println("返回数据3:" + oaId);
                    // System.out.println("发送数据:" + requestInfo);
                    objects = client.invoke(name,requestInfo,Integer.parseInt(oaId));
                    System.out.println("返回数据:" + objects[0]);

                    for (int i=0;i<oaMessages.size();i++){
                        String x=    oaMessages.get(i).getFbk1();
                        String y=    oaMessages.get(i).getKoujnumber();
                        productDao.cutProductNumber(oaMessages.get(i).getFbk1(),oaMessages.get(i).getKoujnumber());
                        MoveHis moveHis=new MoveHis();
                        moveHis.setType("冻结");
                        moveHis.setCommodityNumber(oaMessages.get(i).getFbk1());
                        moveHis.setNumber(oaMessages.get(i).getKoujnumber());
                        moveHis.setUserName(oaMessages.get(0).getNumber());
                        moveHisDao.save(moveHis);
                    }
                    productOrderDao.StartFlow(oaMessages.get(0).getNumber(),objects[0]+"",oaMessages.get(0).getGongyong(),oaMessages.get(0).getDsqr(),oaMessages.get(0).getRemarks(),oaMessages.get(0).getDsqgs()+"-"+oaMessages.get(0).getDsqbm(),oaMessages.get(0).getExpensesBy(),oaMessages.get(0).getProjects());
                    message.setFlag("success");
                    message.setMessage("发起Oa流程成功");
                } catch (Exception e) {
                    message.setFlag("failed");
                    message.setMessage("发起oa流程失败");
                    e.printStackTrace();
                }



            }

        return message;
    }



}
